cmake_minimum_required(VERSION 3.0)
project(test02)

#以下用于屏蔽 error: unrecognized command line option ‘-rdynamic’
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")

#指定编译工具
set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc")
set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++")
set(CMAKE_ASM_COMPILER "aarch64-linux-gnu-gcc")
set(CMAKE_AR "aarch64-linux-gnu-ar")
set(CMAKE_OBJCOPY "aarch64-linux-gnu-objcopy")
set(CMAKE_OBJDUMP "aarch64-linux-gnu-objdump")
set(CMAKE_SIZE "aarch64-linux-gnu-size")

#编译相关选项
set(MCU_FLAGS "-mcpu=cortex-a53 ")
set(CMAKE_C_FLAGS "${MCU_FLAGS} -Wall -Wextra -Werror -Wno-main -nostdlib -fno-builtin  -mgeneral-regs-only") #-w -Wall
set(CMAKE_C_FLAGS_DEBUG "-O0 -g2 -ggdb")
set(CMAKE_C_FLAGS_RELEASE "-O3")
set(PROJECT_NAME "aarch64-qemu")
set(CMAKE_BUILD_TYPE "Debug")


#设置头文件包含目录
include_directories(
    include/
)

#startup文件是STM32CubeMX生成的
ENABLE_LANGUAGE(ASM)
set(SRC_STARTUP "../src/startup.s")
aux_source_directory(src/ MAIN)

#连接生成，ld文件是STM32CubeMX生成的
set(LINKER_SCRIPT "../src/link.ld")
set(CMAKE_EXE_LINKER_FLAGS
" -T${LINKER_SCRIPT}"
)

#生成可执行文件
add_executable(${PROJECT_NAME}.elf ${SRC_STARTUP} ${MAIN})

set(ELF_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf)
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET "${PROJECT_NAME}.elf" POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -Obinary ${ELF_FILE} ${BIN_FILE}
    COMMAND ${CMAKE_OBJCOPY} -Oihex  ${ELF_FILE} ${HEX_FILE}
    COMMENT "Building ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex"

    COMMAND ${CMAKE_COMMAND} -E copy ${BIN_FILE} /home/ubuntu/workspace/aarch64-qemu

    COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
    COMMENT "Invoking: Cross ARM GNU Print Size"
)

